/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package app.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;

import org.rails.extjs.AbsBase;
import org.rails.extjs.ExtJS;
import org.rails.extjs.data.Field;
import org.rails.extjs.data.HttpProxy;
import org.rails.extjs.data.JsonStore;
import org.rails.extjs.json.Json;
import org.rails.extjs.layout.FitLayout;
import org.rails.extjs.panel.Viewport;
import org.rails.extjs.panel.grid.GridPanel;
import org.rails.extjs.panel.grid.PivotAxis;
import org.rails.extjs.panel.grid.PivotGrid;
import org.rails.core.web.Action;
import org.rails.core.web.ControllerBase;

public final class GridPanelController extends ControllerBase {

	public GridPanelController(Action action) throws Exception {
		super(action);
		AbsBase.setDebug(true);
	}
	
	public void pivotGridAction() throws IOException, ServletException{		
		final HttpProxy proxy = new HttpProxy("GET","../GridPanel/privotData.action");
		final JsonStore store = new JsonStore();
		store.setAutoLoad(true);
		store.setProxy(proxy);
		store.setRoot("data");
		store.setProxy(proxy);
		store.addField(new Field("economy","economy"));
		store.addField(new Field("region","region"));
		store.addField(new Field("year","year"));
		store.addField(new Field("product","product"));
		final Viewport view = new Viewport();
		final PivotGrid grid = new PivotGrid();
		grid.setStore(store);
		grid.setWidth(600);
		grid.setHeight(600);
		grid.setMeasure("product");
		grid.setAggregator(PivotGrid.AGGREGATOR_SUM);		
		grid.addLeftAxis(new PivotAxis("region"));
		grid.addLeftAxis(new PivotAxis("economy"));			
		grid.addTopAxis(new PivotAxis("year"));		
		view.setLayout(new FitLayout());
		view.addItem(grid);
		ExtJS.bind(request, view);
		action.render();
	}
	
	public void privotDataAction() throws IOException, ServletException{
		final Json json = new Json();
		final List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
		Map<String,Object> row = new HashMap<String, Object>();		
		row.put("region", "Asia");
		row.put("economy", "Ching");
		row.put("year", "2004");
		row.put("product", 100000);
		data.add(row);
		row = new HashMap<String, Object>();		
		row.put("region", "Asia");
		row.put("economy", "Ching");
		row.put("year", "2005");
		row.put("product", 200000);
		data.add(row);
		row = new HashMap<String, Object>();		
		row.put("region", "Asia");
		row.put("economy", "Ching");
		row.put("year", "2006");
		row.put("product", 306000);
		data.add(row);
		row = new HashMap<String, Object>();		
		row.put("region", "Asia");
		row.put("economy", "Ching");
		row.put("year", "2007");
		row.put("product", 10001200);
		data.add(row);
		json.put("data",data);
		ExtJS.bind(request, json);
		ExtJS.sendToClient(request, action.out());
		
	}
	
	
	public void gridPanel(){
		GridPanel grid = new GridPanel();
	}
	

}
